//
//  HYCtravelSaveTool.m
//  FamilyTravel
//
//  Created by dllo on 15/11/5.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCtravelSaveTool.h"

@implementation HYCtravelSaveTool

+ (void)isHaveFolder {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 2. 拼接电影的文件夹名
    NSString *modelFile = [sandBoxPath stringByAppendingPathComponent:@"HYCLv"];
    // 3. 创建一个文件的管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    // 通过管理者调用方法, 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:modelFile]) {
        [manager createDirectoryAtPath:modelFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

+ (BOOL)isModelInPlist:(HYCTourPalModel *)model {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"HYCLv.plist"];
    
    //反归档
    NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (HYCTourPalModel *tempModel in movieArr) {
        if ([tempModel.number isEqualToString:model.number]) {
            return NO;
        }
    }
    return YES;
}

+ (void) saveModelToPlist:(HYCTourPalModel *)model {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"HYCLv.plist"];
    // 直接归档会覆盖掉之前的数据, 所以先进行反归档, 然后添加进之后再进行归档操作
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (modelArr == nil) {
        modelArr = [NSMutableArray array];
    }
    [modelArr addObject:model];
    // 归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
}

+ (void)removeModelFromPlist:(HYCTourPalModel *)model {
    
}

+ (void)cancelModelInPlist:(HYCTourPalModel *)model {
    // 写路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"HYCLv.plist"];
    // 反归档
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历之后
    for (HYCTourPalModel *tempModel in modelArr) {
        if ([tempModel.number isEqualToString:model.number]) {
            // 移除
            [modelArr removeObject:tempModel];
            break;
        }
    }
    // 归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
}



@end
